<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{name: 'Goods'}">商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加商品</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 步骤条 -->
    <el-steps :active="activeName-0" finish-status="success">
      <el-step title="步骤 1"></el-step>
      <el-step title="步骤 2"></el-step>
      <el-step title="步骤 3"></el-step>
      <el-step title="步骤 4"></el-step>
      <el-step title="步骤 5"></el-step>
    </el-steps>
    <!-- 标签页 -->
    <el-form :model="addForm" :rules="rules" ref="addForm" :label-width="'120px'">
      <el-tabs v-model="activeName" tab-position="left">
        <el-tab-pane label="基本信息" name="0">
          <el-form-item label="商品名称" prop="goods_name">
            <el-input v-model="addForm.goods_name"></el-input>
          </el-form-item>
          <el-form-item label="商品价格" prop="goods_price">
            <el-input v-model.number="addForm.goods_price"></el-input>
          </el-form-item>
          <el-form-item label="商品重量" prop="goods_weight">
            <el-input v-model.number="addForm.goods_weight"></el-input>
          </el-form-item>
          <el-form-item label="商品数量" prop="goods_number">
            <el-input v-model.number="addForm.goods_number"></el-input>
          </el-form-item>
          <el-form-item label="商品分类" prop="selectedOptions">
            <el-cascader
                :options="cateForm"
                v-model="selectedOptions"
                :props="keyRules"
                @change="handleChange"
                clearable
                >
            </el-cascader>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="商品参数" name="1">商品参数</el-tab-pane>
        <el-tab-pane label="商品属性" name="2">商品属性</el-tab-pane>
        <el-tab-pane label="商品图片" name="3">
            <el-upload
              class="upload-demo"
              action="http://localhost:8888/api/private/v1/upload"
              :headers="gettoken()"
              :before-upload="beforeUpload"
              :on-preview="handlePreview"
              :on-success="handleSuccess"
              :on-remove="handleRemove"
              :file-list="fileList2"
              list-type="picture"> <!-- 文件列表显示的风格--照片墙 -->
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
            </el-upload>
        </el-tab-pane>
        <el-tab-pane label="商品内容" name="4">
          <quill-editor
            v-model="addForm.goods_introduce"
            ref="myQuillEditor"
            :options="editorOption"
            style="height:600px;margin:15px"
            @blur="onEditorBlur($event)"
            @focus="onEditorFocus($event)"
            @ready="onEditorReady($event)"
          >
          </quill-editor>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <el-button type="success" round style="float:right;margin-top:20px" @click="addNewGoods">添加商品</el-button>
  </div>
</template>
<script>
import { addNewGoods, getCateList } from '@/api/goods.js'
export default {
  data () {
    return {
      // 富文本相关属性
      editorOption: {

      },
      // 分割线
      activeName: '3',
      addForm: {
        goods_name: '',
        goods_cat: '',
        goods_price: null,
        goods_number: null,
        goods_weight: null,
        goods_introduce: '',
        pics: [],
        attrs: '',
        goods_state: null
      },
      rules: {
        goods_name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' }
        ],
        goods_price: [
          { required: true, message: '请输入商品价格', trigger: 'blur' },
          { type: 'number', message: '请输入数字', trigger: 'blur' }
        ],
        goods_weight: [
          { required: true, message: '请输入商品重量', trigger: 'blur' },
          { type: 'number', message: '请输入数字', trigger: 'blur' }
        ],
        goods_number: [
          { required: true, message: '请输入商品数量', trigger: 'blur' },
          { type: 'number', message: '请输入数字', trigger: 'blur' }
        ],
        selectedOptions: [
          { required: true, message: '请选择商品分类', trigger: 'blur' }
        ]
      },
      //   级联选择器的数据源
      cateForm: [],
      // 级联选择器中选中的数据 -- id数组
      selectedOptions: {},
      keyRules: {
        label: 'cat_name',
        value: 'cat_id',
        children: 'children',
        checkStrictly: true // 设置是否严格遵守父子节点不互相关联 实现我选上一级，下一级没反应，可以只选择上一级
      },
      // 当前所选择上传的文件列表，他是一个数组
      fileList2: []
    }
  },
  mounted () {
    getCateList(3)
      .then(res => {
        // console.log(res)
        if (res.data.meta.status === 200) {
          this.cateForm = res.data.data
        }
      })
      .catch(err => {
        console.log(err)
      })
  },
  methods: {
    // 富文本方法
    // 富文本失焦事件
    onEditorBlur (quill) {
      // console.log('editor blur!', quill)
      // console.log(111)
    },
    // 富文本聚焦事件
    onEditorFocus (quill) {
      // console.log('editor focus!', quill)
      // console.log(222)
    },
    onEditorReady (quill) {
      // console.log('editor ready!', quill)
      // console.log(333)
    },
    onEditorChange ({ quill, html, text }) {
      // console.log('editor change!', quill, html, text)
      this.content = html
      // console.log(444)
    },
    // 分割线
    handleChange (value) {
      // console.log(value) // 通过事件获取当前选中的id
      // console.log(this.selectedOptions) // 通过双向绑定获取当前选中的id
    },
    // 单击预览事件
    handlePreview () {},
    // 移出照片发生事件 -文件列表移除文件时的钩子
    handleRemove (file, fileList) {
      // 添加多张照片，当移出照片再进行上传，此时的上传的照片数组还是有删除的数据
      // console.log(fileList)
      if (!file.response) {
        return false
      }
      //   console.log(file.response.data.tmp_path)
      for (let i = 0; i < this.addForm.pics.length; i++) {
        if (this.addForm.pics[i].pic === file.response.data.tmp_path) {
          this.addForm.pics.splice(i, 1)
        }
      }
    },
    // 文件上传之前的钩子
    beforeUpload (file) {
      // file 里面的文件类型
      // console.log(file)
      if (file.type.indexOf('image/') === -1) {
        this.$message({
          type: 'warning',
          message: '请选择正确的图片格式'
        })
        return false
      } else {
        console.log(file.type)
      }
    },
    // 文件上传成功时的钩子函数
    handleSuccess (response, file, fileList) {
    //   console.log(response)
      if (response.meta.status === 200) {
        this.addForm.pics.push({ pic: response.data.tmp_path })
        // console.log(this.addForm.pics)
      } else {
        this.$message({
          type: 'warning',
          message: response.data.mag
        })
      }
    },
    // 设置上传请求头参数 --token
    gettoken () {
      return { Authorization: window.localStorage.getItem('dwxProject') }
    },
    // 点击添加新产品
    addNewGoods () {
      this.$refs.addForm.validate(valid => {
        if (!valid) {
          console.log('sdasdsdasd')
          return false
        }
      })
      //   console.log(this.addForm.pics)
      this.addForm.goods_cat = this.selectedOptions.join(',')
      addNewGoods(this.addForm)
        .then(res => {
          if (res.data.meta.status === 201) {
            console.log(res)
            this.$message({
              type: 'success',
              message: res.data.meta.msg
            })
          } else {
            this.$message({
              type: 'warning',
              message: res.data.meta.msg
            })
          }
        })
        .catch(err => [
          console.log(err)
        ])
      // console.log(this.addForm.goods_introduce)
    }
  }
}
</script>
<style lang="less" scoped>
</style>
